<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表浏览</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
            min-height: 100vh;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 1200px;
            margin: 0 auto;
            overflow-x: auto;
        }

        h3 {
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }

        .responsive-table {
            width: 100%;
            border-collapse: collapse;
            min-width: 600px;
        }

        .responsive-table th {
            background-color: #54c2e1;
            color: white;
            padding: 12px;
            text-align: left;
            border-bottom: 2px solid #ddd;
        }

        .responsive-table td {
            padding: 12px;
            border-bottom: 1px solid #ddd;
            vertical-align: middle;
        }

        .responsive-table tr:hover {
            background-color: #f5f5f5;
        }

        .product-image {
            width: 100px;
            height: 100px;
            object-fit: contain;
            border-radius: 4px;
            border: 1px solid #ddd;
            padding: 5px;
            background: white;
        }

        @media screen and (max-width: 768px) {
            .container {
                padding: 15px;
            }

            .responsive-table th {
                padding: 8px;
                font-size: 14px;
            }

            .responsive-table td {
                padding: 8px;
                font-size: 14px;
            }

            .product-image {
                width: 60px;
                height: 60px;
            }
        }

        @media screen and (max-width: 480px) {
            .responsive-table th:nth-child(3),
            .responsive-table td:nth-child(3) {
                display: none;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <h3>商品列表浏览</h3>
    <table class="responsive-table">
        <thead>
            <tr>
                <th>商品名称</th>
                <th>商品图片</th>
                <th>商品描述</th>
                <th>商品价格</th>
                <th>商品库存</th>
                <th>商品销量</th>
            </tr>
        </thead>
        <tbody id="container">
       
        </tbody>
    </table>
</div>
</body>
</html>
<script>
    var g_itemList=[];
    jQuery(document).ready(function(){
          $.ajax({
          type:"GET",
          contentType:"application/x-www-form-urlencoded",
          url:"http://localhost:8090/item/list",
          xhrFields: {
        withCredentials: true
    },
          success:function(data){
          if(data.status=="success"){
          g_itemList=data.data;
          reloadDom()
        
          }else{
          alert("获取商品信息失败，原因为"+data.data.errMsg)
          }
          },
          error:function(data){
          alert("获取商品信息失败，原因为"+data.responseText);
          }
          });
          return false;
        });
    function reloadDom(){
        for(var i=0;i<g_itemList.length;i++){
            var itemVO=g_itemList[i];
            var dom = "<tr data-id='"+itemVO.id+"' id='itemDetail"+itemVO.id+"'>"
            + "<td>" + itemVO.title +"</td>"
            + "<td><img src='" + itemVO.imgUrl + "' alt='商品图片' style='width:100px;height:100px;'></td>"  // 商品图片
            + "<td>" + itemVO.description + "</td>"//商品描述
            +"<td>¥" + itemVO.price + "</td>"   //商品价格
            + "<td>" + itemVO.stock + "</td>"    // 商品库存
            + "<td>" + itemVO.sales + "</td>"    // 商品销量
            + "</tr>";

            $("#container").append($(dom));

            $("#itemDetail"+itemVO.id).on("click",function(){
                window.location.href="getItem.html?id="+$(this).data("id");
             });
        }
        
    }
</script>